<!-- 今日加速流量趋势 -->
<template>
  <div>
      <span class="title" v-show="title">{{title}}</span>
      <div ref="linechart" :class="className" :style="{height:height,width:width}" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { formatBytes } from "@/utils/index.js";
require('echarts/theme/macarons') // echarts theme

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: { 
      type: Boolean,
      default: true
    },
    title:{
      type: String,
      default: ''
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
        // console.log(val);
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$refs.linechart, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ time, UpByte, downByte, info } = {}) {
      // console.log(info,111111);
      this.chart.setOption({
        xAxis: {
          data: time,
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          backgroundColor: '#fff',
          textStyle:{
            color:'#000'
          },
          // formatter: '{b}</br>{a}: {c} KB/S</br>{a1}: {c1} KB/S',
          formatter: function name(params) {
            // console.log(params)
            // return `${params[1].name}<br/>${params[1].marker}上行：${params[1].value} KB/S<br/>${params[0].marker}下载：${params[0].value} KB/S`
            // return `${params[1].marker}上行：${formatBytes(params[0].value)}/S<br/>${params[0].marker}下载：${formatBytes(params[0].value)}/S`
            return `${params[0].marker}总数：${params[0].value.toFixed(2)}%`
          },
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisLabel: {
            formatter: function(value) {
              return value.toFixed(2) + ' %';
            },
          },
          axisTick: {
            show: false
          }
        },
        legend: {
          data: [info,  '单位(%使用率)'],
          selectedMode: false
        },
        series: [
          {
            name: info,
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  // color: 'rgba(24, 144, 255, .2)'
                }
              }
            },
            symbol: "none",
            data: downByte,
            animationDuration: 2800,
            animationEasing: 'quadraticOut',
            areaStyle:{}
          },
          {
            name:'单位(%使用率)',
            smooth: true,
            type: 'bar'
          }
        ]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
